package club.emergency.wechat.manager.operate.provider.impl;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.manager.operate.ManagerOperate;
import club.emergency.wechat.model.manager.Manager;
import club.emergency.wechat.persistence.manager.ManagerMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.manager.operate.provider.impl
 * Date: 2018/12/5
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@RestController
public class ManagerOperateImpl implements ManagerOperate {

    private ManagerMapper managerMapper;

    public ManagerOperateImpl(ManagerMapper managerMapper) {
        this.managerMapper = managerMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String save(@RequestBody Manager manager) {
        managerMapper.insert(manager);
        Long id = manager.getId();
        return ResponseEntity.ok(StatusCode.DB_SAVE_OK, "数据存储成功", id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String modify(Manager manager) {
        managerMapper.updateById(manager);
        return ResponseEntity.ok(StatusCode.DB_MODIFY_OK, "数据修改成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String removeById(@RequestParam("id") Long id) {
        //删除成功记录数
        int i = managerMapper.deleteById(id);
        if (i == 0) {
            return ResponseEntity.error(StatusCode.DB_REMOVE_ERROR, "管理员数据不存在，请确认是否有改数据？");
        }
        return ResponseEntity.ok(StatusCode.DB_REMOVE_OK, "数据删除成功");
    }
}
